package com.platform.usercenter.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.collection.CollectionUtil;
import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.log.enums.BusinessType;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.CalHoliday;
import com.platform.usercenter.domain.SysOrganizationRelation;
import com.platform.usercenter.service.ICalHolidayService;
import com.platform.usercenter.service.ISysOrganizationRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 节假日设置Controller
 *
 * @author yinjinlu
 * @date 2022-06-08
 */
@RestController
@RequestMapping("/mes/cal/calholiday")
public class CalHolidayController extends BaseController
{
    @Resource
    private ICalHolidayService calHolidayService;
    @Resource
    ISysOrganizationRelationService organizationRelationService;

    /**
     * 查询节假日设置列表
     */
    @GetMapping("/list")
    public AjaxResult list( CalHoliday calHoliday)
    {
        Long orgId=calHoliday.getOrgId( );
        if(Objects.isNull(orgId)){
            throw new RuntimeException( "orgId 查询参数缺失" );
        }else {
            //根据组织id查询下级组织的组织id
            // 查询下游组织信息-实际关系
            SysOrganizationRelation relationQuery = new SysOrganizationRelation();
            relationQuery.setParentId(orgId);
            relationQuery.setRelation( SysUserConstants.ORG_REAL);
            relationQuery.setIsDelete(SysUserConstants.N);
            List<SysOrganizationRelation> realOrgList = organizationRelationService.selectSysOrganizationRelationList(relationQuery);
            if (CollectionUtil.isNotEmpty(realOrgList)) {
                List< Long > collect=realOrgList.stream( ).map( SysOrganizationRelation::getOrgId )
                        .collect( Collectors.toList( ) );
                collect.add( orgId );
                calHoliday.setOrgIdList( collect );
                calHoliday.setOrgId(null);
            }
        }
        List<CalHoliday> list = calHolidayService.selectCalHolidayList(calHoliday);
        return AjaxResult.success(list);
    }

    /**
     * 导出节假日设置列表
     */

    @PostMapping("/export")
    public void export(HttpServletResponse response, CalHoliday calHoliday)
    {
        List<CalHoliday> list = calHolidayService.selectCalHolidayList(calHoliday);
        ExcelUtil<CalHoliday> util = new ExcelUtil<CalHoliday>(CalHoliday.class);
        util.exportExcel(response, list, "节假日设置数据");
    }

    /**
     * 获取节假日设置详细信息
     */

    @GetMapping(value = "/{holidayId}")
    public AjaxResult getInfo(@PathVariable("holidayId") Long holidayId)
    {
        return AjaxResult.success(calHolidayService.selectCalHolidayByHolidayId(holidayId));
    }

    /**
     * 新增节假日设置
     */
    @PostMapping
    public AjaxResult add(@RequestBody CalHoliday calHoliday)
    {
        CalHoliday param = new CalHoliday();
        param.setTheDay(calHoliday.getTheDay());
        List<CalHoliday> days = calHolidayService.selectCalHolidayList(param);
        if(CollUtil.isNotEmpty(days)){
            calHoliday.setHolidayId(days.get(0).getHolidayId());
            return toAjax(calHolidayService.updateCalHoliday(calHoliday));
        }else{
            return toAjax(calHolidayService.insertCalHoliday(calHoliday));
        }
    }

    /**
     * 修改节假日设置
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CalHoliday calHoliday)
    {
        return toAjax(calHolidayService.updateCalHoliday(calHoliday));
    }

    /**
     * 删除节假日设置
     */
	@DeleteMapping("/{holidayIds}")
    public AjaxResult remove(@PathVariable Long[] holidayIds)
    {
        return toAjax(calHolidayService.deleteCalHolidayByHolidayIds(holidayIds));
    }
}
